package cn.itcast.web.controller.cargo;


import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.ExtCproductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.web.controller.BaseController;
import cn.itcast.web.utils.FileUploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("/cargo/extCproduct")
public class ExtCproductController extends BaseController {
    @Reference
    private ExtCproductService extCproductService;

    @Reference
    private FactoryService factoryService;

    @Autowired
    private FileUploadUtil fileUploadUtil;

    /*
    url：/cargo/extCproduct/list.do?
        contractId=79606e2e-e34b-4c72-9645-762f6bf434fe
        &contractProductId=872aee8b-0fea-4b52-beba-b69fd28b4298
    参数：购销合同id   货物id
    作用：进入附件列表   添加附件页面
    */
    @RequestMapping("/list")
    public String findAll(String contractId,String contractProductId,@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "5") Integer pageSize){
        //查询生产附件的工厂
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria factoryCriteria = factoryExample.createCriteria();
        factoryCriteria.andCtypeEqualTo("附件").andCompanyIdEqualTo(getLoginUserCompanyId());
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);

        //根据货物ID查询附件列表
        ExtCproductExample extCproductExample = new ExtCproductExample();
        ExtCproductExample.Criteria productCriteria = extCproductExample.createCriteria();
        productCriteria.andContractProductIdEqualTo(contractProductId);
        PageInfo<ExtCproduct> pageInfo = extCproductService.findByPage(extCproductExample, pageNum, pageSize);
        request.setAttribute("pageInfo",pageInfo);

        //3. 购销合同的id   货物id
        request.setAttribute("contractId",contractId);
        request.setAttribute("contractProductId",contractProductId);

        return "cargo/extc/extc-list";
    }

    /*
          url:/edit.do
          作用： 保存、修改 附件信息
          参数： extCproduct
          返回值：附件列表页面
         */
    @RequestMapping("/edit")
    public String edit(ExtCproduct extCproduct, MultipartFile productPhoto) throws Exception {
        //附件的创建人
        extCproduct.setCreateBy(getLoginUser().getId());
        //附件的创建人所属的部门
        extCproduct.setCreateDept(getLoginUser().getDeptId());
        //附件的创建人所属的企业id
        extCproduct.setCompanyId(getLoginUserCompanyId());
        //附件的创建人所属的企业名称
        extCproduct.setCompanyName(getLoginUserCompanyName());

        if (!productPhoto.isEmpty()){
            String url = fileUploadUtil.upload(productPhoto);
            extCproduct.setProductImage("http://"+url);
        }

        if (StringUtils.isEmpty(extCproduct.getId())){
            //如果id为空，添加附件信息
            extCproductService.save(extCproduct);
        }else {
            //页面有传id，修改附件信息
            System.out.println("========"+extCproduct);
            extCproductService.update(extCproduct);
        }
        return "redirect:/cargo/extCproduct/list.do?contractId="+extCproduct.getContractId()+"&contractProductId="+extCproduct.getContractProductId();
    }

    /*
          url:/cargo/extCproduct/toUpdate.do?
          作用： 进入附件修改页面，信息回显  1、生产附件的厂家  2、附件信息
          参数： 附件id  购销合同id   货物id
         */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id,String contractId,String contractProductId){
        //1、查询生产附件的工厂
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件").andCompanyIdEqualTo(getLoginUserCompanyId());
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);

        //2、附件信息
        ExtCproduct extCproduct = extCproductService.findById(id);
        request.setAttribute("extCproduct",extCproduct);

        //3. 购销合同的id   货物id
        request.setAttribute("contractId",contractId);
        request.setAttribute("contractProductId",contractProductId);
        return "cargo/extc/extc-update";
    }

    /*
    url:/cargo/extCproduct/delete.do?
        id=${o.id}&
        contractId=${contractId}&
        contractProductId=${o.contractProductId}
    参数： 附件id  购销合同id   货物id
    作用：删除某个附件
    返回值 : 附件列表
     */
    @RequestMapping("/delete")
    public String delete(String id,String contractId,String contractProductId){
        extCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list.do?contractId="+contractId+"&contractProductId="+contractProductId;
    }



}
